package com.ruoyi.base.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.base.domain.BaseCourse;
import com.ruoyi.base.service.IBaseCourseService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Api("课程信息管理")
@RestController
@RequestMapping("/base/course")
public class BaseCourseController extends BaseController
{
    @Autowired
    private IBaseCourseService baseCourseService;

    /**
     * 查询课程列表
     */
    @ApiOperation("获取课程列表")
    @PreAuthorize("@ss.hasPermi('base:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseCourse baseCourse)
    {
        startPage();
        List<BaseCourse> list = baseCourseService.selectBaseCourseList(baseCourse);
        return getDataTable(list);
    }
    

    /**
     * 查询课程列表
     */
    @ApiOperation("获取课程列表")
    @GetMapping("/select")
    public TableDataInfo select(BaseCourse baseCourse)
    {
        List<BaseCourse> list = baseCourseService.selectBaseCourseList(baseCourse);
        return getDataTable(list);
    }


    /**
     * 导出课程列表
     */
    @ApiOperation("导出课程列表")
    @PreAuthorize("@ss.hasPermi('base:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BaseCourse baseCourse)
    {
        List<BaseCourse> list = baseCourseService.selectBaseCourseList(baseCourse);
        ExcelUtil<BaseCourse> util = new ExcelUtil<BaseCourse>(BaseCourse.class);
        return util.exportExcel(list, "课程");
    }

    /**
     * 获取课程详细信息
     */
    @ApiOperation("获取课程详细信息")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseCourseService.selectBaseCourseById(id));
    }

    /**
     * 新增课程
     */
    @ApiOperation("新增课程")
    @PreAuthorize("@ss.hasPermi('base:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseCourse baseCourse)
    {
        return toAjax(baseCourseService.insertBaseCourse(baseCourse));
    }

    /**
     * 修改课程
     */
    @ApiOperation("修改课程")
    @PreAuthorize("@ss.hasPermi('base:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseCourse baseCourse)
    {
        return toAjax(baseCourseService.updateBaseCourse(baseCourse));
    }

    /**
     * 删除课程
     */
    @ApiOperation("删除课程")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseCourseService.deleteBaseCourseByIds(ids));
    }
    
    /**
     * 禁用课程
     */
    @ApiOperation("禁用课程")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
	@GetMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids)
    {
        return toAjax(baseCourseService.updateBaseCourseStatus(ids, "0"));
    }
    
    /**
     * 启用课程
     */
    @ApiOperation("启用课程")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
	@GetMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids)
    {
        return toAjax(baseCourseService.updateBaseCourseStatus(ids, "1"));
    }
    
    @Log(title = "导入课程信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseCourse> util = new ExcelUtil<BaseCourse>(BaseCourse.class);
        List<BaseCourse> courseList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = baseCourseService.importCourse(courseList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<BaseCourse> util = new ExcelUtil<BaseCourse>(BaseCourse.class);
        return util.importTemplateExcel("课程数据");
    }
}
